package com.xinshengli.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 日期工具类
 */
public class DateUtil {

    /**
     *
     * 2.	求未来日期离今天还剩的天数
     * 3.	求过去日期离今天还剩的天数
     * 4.	判断给定的日期是否为今天
     * 5.	判断给定的日期是否在本周之内
     * 6.	判断给定的日期是否在本月之内
     * 7.	给定时间对象，初始化到该月初的1月1日0时0分0秒0毫秒
     * 8.	给定时间对象，设定到该月最一天的23时59分59秒999毫秒
     * 9.	时间比较
     */

    /**
     * 将字符串类型的日期转换成Date类型的日期
     * @param string
     * @return
     */
    public static Date stringToDate(String string){

        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(string);
            return date;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 1.	根据日期算年龄
     * @param string
     * @return
     */
    public static int getAge(String string){
        //获取现在的年份   和   出生的年份   最后相减

        //现在的日期
        Calendar calendar = Calendar.getInstance();
        int nowYear = calendar.get(Calendar.YEAR);//获取年份

        //生日的日期
        Date birthday = stringToDate(string);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(birthday);
        int oldYear = calendar1.get(Calendar.YEAR);//获取年份

        return nowYear-oldYear;
    }

    /**
     * 1.	根据日期算年龄
     * @param date
     * @return
     */
    public static int getAge(Date date){
        //现在的日期
        Calendar calendar = Calendar.getInstance();
        int nowYear = calendar.get(Calendar.YEAR);//获取年份


        //生日的日期
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date);
        int oldYear = calendar1.get(Calendar.YEAR);//获取年份

        return nowYear-oldYear;
    }

    public static void main(String[] args) throws ParseException {


    }

    /**
     * 3.	求未来日期离今天还剩的天数
     * @param future
     * @return
     */
    public static int getFutureDays(String future) throws ParseException {
        //当前日期
        long zero = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();

        //未来日期
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        long time = dateFormat.parse("2020-09-29").getTime()-zero;

        return (int)(time/1000/60/60/24);
    }

   //

    /**
     * 9.	时间比较
     *         第一个参数大于第二个参数  返回true   否则返回false
     * @param start
     * @param end
     * @return
     */
    public static boolean timeComparison(Date start , Date end){
        return  start.getTime() - end.getTime() >0;
    }
    /**
     * 判断给定日期是否是当天
     * @param str
     * @return
     */
    public static boolean isToday(String str) throws ParseException {
		/*
		 * 获取日期的年 月 日
		 * Calendar instance = Calendar.getInstance();
		instance.setTime(new Date());
		instance.get(Calendar.YEAR);
		instance.get(Calendar.MONTH);
		instance.get(Calendar.DAY_OF_MONTH);*/

        int futureDays = getFutureDays(str);
        if(futureDays==0) {
            return true;
        }
        return false;
    }
    /**
     * 判断当前给定的日期是否在本周之内
     * @return
     */
    public static boolean isDayInWeek(String date) {

        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        int dayOfWeek = instance.get(Calendar.DAY_OF_WEEK_IN_MONTH);

        Date stringToDate = stringToDate(date);
        Calendar dateToCalendar = dateToCalendar(stringToDate);
        int year1 = dateToCalendar.get(Calendar.YEAR);
        int month1 = dateToCalendar.get(Calendar.MONTH);
        int dayOfWeek1 = dateToCalendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);

        if(year==year1&&month==month1&&dayOfWeek==dayOfWeek1) {
            return true;
        }
        return false;
    }

    /**
     * 将Date转换成Calendar类型
     * @param date
     * @return
     */
    public static Calendar dateToCalendar(Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        return instance;
    }
    /***
     * 给定时间对象，初始化到该年初的1月1日0时0分0秒0毫秒
     */
    public static void  setInitDate(String str) {
        Date stringToDate = stringToDate(str);
        Calendar dateToCalendar = dateToCalendar(stringToDate);
        dateToCalendar.set(Calendar.MONTH, 1);
        dateToCalendar.set(Calendar.DAY_OF_YEAR, 1);
        dateToCalendar.set(Calendar.HOUR, 0);
        dateToCalendar.set(Calendar.MINUTE, 0);
        dateToCalendar.set(Calendar.SECOND, 0);
        dateToCalendar.set(Calendar.MILLISECOND, 0);

        System.out.println(dateToCalendar.getTime());

    }


    /**方法2：根据传入的参数获取该日期的月初日期，例如给定的日期为“2019-09-19 19:29:39”，返回“2019-09-01 00:00:00”*/
    public static Date getDateByMonthInit (Date date) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int year = ca.get(Calendar.YEAR);
        int mouth = ca.get(Calendar.MONTH);
        ca.clear();
        ca.set(Calendar.YEAR, year);
        ca.set(Calendar.MONTH, mouth);
        ca.set(Calendar.HOUR, 0);
        return ca.getTime();
    }

    /**方法3 :根据传入的参数获取该日器的月末日期，例如给定的日期为“2019-09-19 19:29:39”，返回“2019-09-30 23:59:59”，注意月大月小以及闰年。*/
    public static Date getDateByMonthLast(Date date){
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int year = ca.get(Calendar.YEAR);
        int mouth = ca.get(Calendar.MONTH);
        ca.clear();
        ca.set(Calendar.HOUR, 0);
        ca.set(Calendar.YEAR, year);
        ca.set(Calendar.MONTH, mouth+1);
        Long time = ca.getTime().getTime();
        return new Date(time - 1);
    }

    /** 随机一个时间  param:int类型的年份，随机日期在该年份之后*/
    public static Date getRandomDate(int year){
        Calendar now = Calendar.getInstance();
        int nowYear = now.get(Calendar.YEAR);
        int nowMouth = now.get(Calendar.MONTH)+1;
        int nowDay = now.get(Calendar.DATE);

        Random rd = new Random();
        int newYear = rd.nextInt(nowYear - year+1)+year;
        int newMonth = 0;
        if(newYear == year){
            newMonth = rd.nextInt(nowMouth);
        }else{
            newMonth = rd.nextInt(13);
        }
        //newMonth = newMonth==0?newMonth+1:newMonth;
        int newDay = 0;
        if(newYear == year && newMonth==nowMouth){
            newDay = rd.nextInt(nowDay+1);
        }else{
            if(newMonth!=4){
                newDay = rd.nextInt(31);
            }else{
                newDay = rd.nextInt(29);
            }
        }
        newDay = newDay==0?newDay+1:newDay;
        Date date = new Date(rd.nextInt(1000*60*60*24));
        now.setTime(date);
        now.set(newYear, newMonth, newDay);
        return now.getTime();
    }

}
